/**
 * This file is part of iSecurePasswords.
 *
 * iSecurePasswords is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * iSecurePasswords is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * iSecurePasswords. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Matthew MacGregor Created: 2012 iSecurePasswords, copyright 2013
 * Sudolink, LLC
 *
 */
package com.sudolink.isp.ui.locktimer;

import com.sudolink.isp.util.HelperMethods;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Implements the ITime interface to provide a timer for locking the user 
 * interface.
 * 
 * @author Matthew
 */
public class LockTimer implements ITime {
    
    private int secondsUntilLocked = 180;
    private IDoSomething listener;
    private final Timer timer;
    private TimerTask task;
    
    public LockTimer() {
        timer = new Timer();  
    }
    
    /**
     * The duration of time between the start of the timer and when the lock
     * will be applied.
     * 
     * @param seconds Seconds until locked.
     */
    public void setSecondsUntilLocked( int seconds ) {
        secondsUntilLocked = seconds;
    }

    /**
     * The duration of time between the start of the timer and when the lock
     * will be applied.
     * 
     * @param millis Milliseconds until locked.
     */
    public void setMillisecondsUntilLocked( int millis ) {
        secondsUntilLocked = millis / 1000;
    }
    
    /**
     * Starts the timer. It is safe to call this more than once, but if there
     * is an existing timer, it will be canceled and a new timer created. This
     * has the same effect as calling restart().
     */
    @Override
    public void start() {
        stop();
        task = new TimerTask() {
            @Override
            public void run() {
                if( listener != null ) {
                    listener.onAction();
                }
            }
        };
        timer.schedule(task, HelperMethods.secondsToMilliseconds(secondsUntilLocked));  
    }

    /**
     * Stops the timer.
     */
    @Override
    public void stop() {
        if( task != null ) {
            task.cancel();
            task = null;
        }
    }

    /**
     * Sets an optional listener, that will be fired when the timer completes.
     * @param listener 
     */
    @Override
    public void setListener( IDoSomething listener ) {
        this.listener = listener;
    }

    /**
     * Gets the optional listener.
     * @return 
     */
    @Override
    public IDoSomething getListener() {
        return listener;
    }
    
}
